package com.wxw.BaseLibrary.tool;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Vibrator;
import android.support.v4.app.NotificationCompat;

import java.io.File;

/**
 * @author david
 * @date 2020-03-30
 */
public class NotificationUtil {
    private static NotificationManager notificationManager = null;
    private static NotificationUtil notificationUtil = null;
    private int index = 1;
    private long vibrateTime = 200;
    private boolean canVibrate = true;
    private Context context;
    private Vibrator vibrator;

    private NotificationUtil(Context context) {
        this.context = context;
        notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        vibrator = (Vibrator)this.context.getSystemService(Context.VIBRATOR_SERVICE);
    }

    public static NotificationUtil getInstance(Context context) {
        if (notificationUtil == null) {
            notificationUtil = new NotificationUtil(context);
        }
        return notificationUtil;
    }

    public void showBigText(String title, String bigText, long when, int smallIconId, Bitmap largeIcon,
                            String soundPath, int requestCode, int flags, Intent intent) {
        if (notificationManager == null) {
            return;
        }
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setContentTitle(title)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(bigText))
                .setWhen(when)
                .setSmallIcon(smallIconId)
                .setPriority(Notification.PRIORITY_HIGH)
                .setCategory(Notification.CATEGORY_ALARM)
                .setAutoCancel(true);
        Notification notification = buildNotification(builder, largeIcon, intent, requestCode, flags, soundPath);
        notificationManager.notify(index++, notification);
        vibrate();
    }

    public void showBigText(String title, String bigText, int smallIconId, Bitmap largeIcon, String soundPath,
                            int requestCode, int flags, Intent intent) {
        showBigText(title, bigText, System.currentTimeMillis(), smallIconId, largeIcon, soundPath,
                requestCode, flags, intent);
    }

    public void show(String title, String text, long when, int smallIconId,
                     Bitmap largeIcon, String soundPath, int requestCode, int flags, Intent intent) {
        if (notificationManager == null) {
            return;
        }
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setContentTitle(title)
                .setContentText(text)
                .setWhen(when)
                .setSmallIcon(smallIconId)
                .setPriority(Notification.PRIORITY_HIGH)
                .setCategory(Notification.CATEGORY_ALARM)
                .setAutoCancel(true);
        Notification notification = buildNotification(builder, largeIcon, intent, requestCode, flags, soundPath);
        notificationManager.notify(index++, notification);
        vibrate();
    }

    public void show(String title, String text, long when, int smallIconId, Bitmap largeIcon, String soundPath) {
        show(title, text, when, smallIconId, largeIcon, soundPath, 0, 0, null);
    }

    public void show(String title, String text, long when, int smallIconId, Bitmap largeIcon) {
        show(title, text, when, smallIconId, largeIcon, "");
    }

    public void show(String title, String text, int smallIconId, Bitmap largeIcon) {
        show(title, text, System.currentTimeMillis(), smallIconId, largeIcon);
    }

    public void show(String title, String text, int smallIconId) {
        show(title, text, smallIconId, null);
    }

    public void vibrate(long milliseconds) {
        if (vibrator != null && canVibrate) {
            vibrator.vibrate(milliseconds);
        }
    }

    public void vibrate() {
        vibrate(vibrateTime);
    }

    public long getVibrateTime() {
        return vibrateTime;
    }

    public void setVibrateTime(long vibrateTime) {
        this.vibrateTime = vibrateTime;
    }

    public boolean isCanVibrate() {
        return canVibrate;
    }

    public void setCanVibrate(boolean canVibrate) {
        this.canVibrate = canVibrate;
    }

    private Notification buildNotification(NotificationCompat.Builder builder, Bitmap largeIcon, Intent intent,
                                           int requestCode, int flags, String soundPath) {
        if (largeIcon != null) {
            builder.setLargeIcon(largeIcon);
        }
        if (intent != null) {
            builder.setContentIntent(PendingIntent.getActivity(context, requestCode, intent, flags));
        }
        if (!"".equals(soundPath)) {
            builder.setSound(Uri.fromFile(new File(soundPath)));
        }
        return builder.build();
    }
}
